// 3：大数相减 求差
/**
 * 899 200  // 699
 * 200 899  // 699
 * 
 * 1. 比较  比较出较大值，较大值 - 较小值
 * 2. 处理大数的减法逻辑
 * * */
// bigIntDeci('999','1000')
export function bigIntDeci(a,b) {
    if(a === b) return 0;

    a = a.split('').map(Number).reverse();
    b = b.split('').map(Number).reverse();

    let result = [];
    // 大数后面的位数时0 ，要记录一个补位，0 => 10
    let count = 0;
    let borrow = 0;

    let i = 0;
    let j = 0;
  // 1000 9999
    while (i < a.length || j < b.length) {
      let d1 = i < a.length? a[i] : 0;
      let d2 = j < b.length? b[j] : 0;
        
      // 处理
      if(d1 < 0) {
        d1 += 10 
        borrow = 1
      }else {
        borrow = 0
      }
      let diff = d1 - d2 - borrow ;
      result.push(diff);
  
      i++;
      j++;
    }

    while (result[result.length - 1] === 0 && result.length > 1) {
        result.pop();
    }

    return result.reverse().join('')

}


function subtractLargeNumbers(a, b) {
    a = a.split('').map(Number).reverse();
    b = b.split('').map(Number).reverse();
  
    let result = [];
    let borrow = 0;
  
    let i = 0;
    let j = 0;
  
    while (i < a.length || j < b.length) {
      let digit1 = i < a.length? a[i] : 0;
      let digit2 = j < b.length? b[j] : 0;
  
      let diff = digit1 - digit2 - borrow;
      if (diff < 0) {
        diff += 10;
        borrow = 1;
      } else {
        borrow = 0;
      }
  
      result.push(diff);
  
      i++;
      j++;
    }
  
    // ？
    while (result[result.length - 1] === 0 && result.length > 1) {
      result.pop();
    }
  
    return result.reverse().join('');
  }
  
  let num1 = '12345678901234567890';
  let num2 = '98765432109876543210';
  
  console.log(subtractLargeNumbers(num1, num2));
  


